﻿#include "ssWXConsole.h"


ssWXConsole::ssWXConsole( wxWindow *parent, const wxPoint& pos, const wxSize& size )
	:wxFrame(parent, wxID_ANY, wxT("console ^_^"), pos, size, wxCAPTION|wxSYSTEM_MENU|wxMINIMIZE_BOX|wxMAXIMIZE_BOX|wxRESIZE_BORDER)
{
	wxArrayString str;
	str.Add(wxT(">控制台初始化..."));
	wxSize sz = GetClientSize();
	sz.y -= 20;
	wxMenuBar* menuBar =new wxMenuBar;
	wxMenu* menu = new wxMenu;
	menu->Append(13, wxT("SaveFile"));
	menu->Append(14, wxT("ClearText"));
	menuBar->Append(menu, wxT("Function"));
	SetMenuBar(menuBar);
	m_list.Create(this, 10, wxPoint(0,0), wxSize(sz.x, sz.y-20), str, wxHSCROLL);
	m_text.Create(this, 11, wxT(""), wxPoint(0,sz.y-20), wxSize(sz.x-40, 20));
	m_in.Create(this, 12, wxT("enter"), wxPoint(sz.x-40,sz.y-20), wxSize(40, 20));
}

ssWXConsole::~ssWXConsole()
{
}

BEGIN_EVENT_TABLE(ssWXConsole, wxFrame)
	EVT_MENU(14, ssWXConsole::OnClear)
	EVT_BUTTON(12, ssWXConsole::OnEnter)
	EVT_SIZE(ssWXConsole::OnSize)
END_EVENT_TABLE()

void ssWXConsole::OnClear(wxCommandEvent& event)
{
	m_list.Clear();
}

void ssWXConsole::ShowMessage( const char* str )
{
	::wxMessageBox(wxString(str, wxConvLocal));
}

void ssWXConsole::Print( const char* str )
{
	wxString wxstr(">", wxConvLocal);
	wxstr += str;
	m_list.Insert(wxstr, m_list.GetCount());
#ifdef _WIN32
	OutputDebugStringA(wxstr.c_str());
#endif
}

void ssWXConsole::Print( wxString& str )
{
	m_list.Insert(str, m_list.GetCount());
}

void ssWXConsole::Printf(const char *strf, ...)
{
	char tmpStr[1024] = { 0 };
	// 处理变长参数
	va_list arglist;
	va_start(arglist, strf);
	vsnprintf(tmpStr, 1024, strf, arglist);
	va_end(arglist);

	Print(tmpStr);
}

void ssWXConsole::OnEnter( wxCommandEvent& event )
{
	m_list.Insert(m_text.GetValue(), m_list.GetCount());
}

void ssWXConsole::OnSize(wxSizeEvent& event)
{
	wxSize sz = GetClientSize();
	m_list.SetSize(0,0,sz.x,sz.y-20);
	m_text.SetSize(0, sz.y - 20, sz.x - 40, 20);
	m_in.SetSize(sz.x - 40, sz.y - 20, 40, 20);
}

void ssWXConsole::Create(wxWindow *parent, const wxPoint& pos, const wxSize& size)
{
	Delete();
	ssWXConsole* pc = new ssWXConsole(parent, pos, size);
	pc->Show(true);
	g_pConsole = pc;
}

void ssWXConsole::Delete()
{
	if (g_pConsole)
	{
		delete g_pConsole;
		g_pConsole = 0;
	}
}
